/* 
* Copyright (c) 2021-2023 Alibaba Cloud Computing Ltd. 
* SPDX-License-Identifier: MulanPSL-2.0 
*/ 
export const resetTableData = (data: string) => {
  if (!data) return []
  // 处理 [WARNING]
  let pos = -1
  if (data.indexOf('[WARNING]') >= 0) {
    pos = data.indexOf('[WARNING]')
  } else if (data.indexOf('[Warning]') >= 0) {
    pos = data.indexOf('[Warning]')
  } else if (data.indexOf('[warning]') >= 0) {
    pos = data.indexOf('[warning]')
  }
  const strValid = pos === -1? data: data.substring(0, pos)

  // 有效字符串
  const strText = '\n'+ strValid
  const list =  strText?.split('\n- ')?.filter((key: any)=> key)
  const dataSource: any = []
  if (Array.isArray(list)) {
    list.forEach((item: any, i: any) => {
      const index = item.indexOf('\n')
      if (index !== -1) {
        const parentCatalogue = item.slice(0, index)
        const childList = item.slice(index+2).split('\n ')?.filter((key: any)=> key)

        const children = childList?.map((key: any, j: any) => {
          const row = key.replace('[', '').replace(']', '').split(' ') 
          return {
            id: `${i+1}-${j+1}`,
            status: row[0],
            name: row[1],
            //
            parentName: parentCatalogue,
          }
        });
         
        dataSource.push({
          id: `${i+1}`,
          name: parentCatalogue,
          status: '',
          childDataSource: children,
          involved: !!children?.filter((r: any)=> r.status === "active")?.length,
          count: children?.length || 0,
        })
      } else {
        dataSource.push({
          id: `${i+1}`,
          name: item,
          status: '',
          involved: false,
          count: 0,
        })
      }
    });
  }
  return dataSource
}